<?php


class groupModel extends Model{
	
	//操作的本类表名
	private $tableName = 'group';
	private $userTable = 'user';
	
	/**
	 * 查询全部权限组信息
	 */
	public function findAll($limit=0, $num=20){
		
		$sql = 'select g.*, u.username as createName from '.tblName($this->tableName).' g LEFT JOIN '.tblName($this->userTable).' u ON ( g.createUser = u.id) ORDER BY g.ID ASC  LIMIT '.$limit.' , '.$num;
		
		try {
			$groupList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $groupList;
		
	}
	
	/**
	 * 统计权限组总数量
	 * 
	 * @param unknown_type $type
	 */
	public function countAll(){
		
		$sql = 'select count(*) as num from '.tblName($this->tableName);
		
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	/**
	 * 查询单个权限组信息，通过权限组编号进行查询
	 * 
	 * @param int $id 权限组编号
	 */
	public function findOne($id){
		
		$sql = 'select * from '.tblName($this->tableName).' WHERE ID = \''.$id.'\'';
		
		try {
			$groupInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $groupInfo;
	}
	
	/**
	 * 进行权限组新增
	 * 
	 * @param Array $array
	 * @param int $returnID
	 */
	public function add($array, $returnID = 1){
		
		$sql = insertTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0 && $returnID == 1) {
			return $this->db->lastInsertID;
		}else {
			throw new Exception('权限组添加失败。');
			return -1;
		}
	}
	
	/**
	 * 进行修改操作
	 * 
	 * @param Array $array
	 * @param Array $where
	 */
	public function modify($array, $where){
		$sql = updateTableSql($this->tableName, $array, $where);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	/**
	 * 进行删除操作
	 * 
	 * @param Array $array
	 */
	public function delete($array){
		
		$sql = deleteTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
} 

?>
